import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:ls_work/model/medicine_advice_model.dart';
import 'package:ls_work/ui/medicine/receive_medicine/add_receive_medicine_page.dart';

import '../../../net/response/medicine_advice/receive_medicine_record_list.dart';
class ReceiveMedicineListViewModel extends BaseListViewModel<ReceiveMedicineRecordList>{
  ReceiveMedicineListViewModel(super.context);

  ///长者姓名
  String? searchName;

  ///房间号
  String? roomName;

  ///药品名称
  String? medicineName;

  ///收药类型
  PickerDataWithIdModel? selectedType;

  ///入柜状态
  PickerDataWithIdModel? receiveStatus;

  var inBatchMode = false;

  List<ReceiveMedicineRecordList> chooseItem = [];


  var model = MedicineAdviceModel();

  @override
  Future<List<ReceiveMedicineRecordList>> requestListData() async {
    return await model.receiveMedicineRecord(customerNameLike: searchName,bedNoLike: roomName,drugNameLike: medicineName,bizType: selectedType?.pickId,status: receiveStatus?.pickId,page: page,rows: pageLength);
  }

  void chooseReceiveType(){
    var items = [
      PickerDataWithIdModel(pickId: '0',pickName: '医嘱收药'),
      PickerDataWithIdModel(pickId: '1',pickName: '自带药入柜'),
    ];
    DataPickerView(context, items,currentFirstData: selectedType,onSubmitListener: (p1,p2,p3){
      selectedType = items[p1];
      notifyListeners();
      requestRefresh();
    }).show();
  }

  void chooseStatus(){
    var items = [
      PickerDataWithIdModel(pickId: '0',pickName: '待入柜'),
      PickerDataWithIdModel(pickId: '1',pickName: '已入柜'),
    ];
    DataPickerView(context, items,currentFirstData: receiveStatus,onSubmitListener: (p1,p2,p3){
      receiveStatus = items[p1];
      notifyListeners();
      requestRefresh();
    }).show();
  }

  @override
  void onCreated() {
    super.onCreated();
    requestRefresh();
  }

  void changeToBatchMode(){
    inBatchMode = true;
    notifyListeners();
  }

  void cancelBatchModel(){
    inBatchMode = false;
    chooseItem.clear();
    notifyListeners();
  }

  void batchItemClick(ReceiveMedicineRecordList item){
    if(chooseItem.contains(item)){
      chooseItem.remove(item);
    }else{
      chooseItem.add(item);
    }
    notifyListeners();
  }


  void allChoose(){
    chooseItem.clear();
    chooseItem.addAll(listItems);
    notifyListeners();
  }

  bool get isAllChoose => listItems.length==chooseItem.length;

  void resetClick() {
    searchName = null;
    medicineName = null;
    selectedType = null;
    receiveStatus = null;
    notifyListeners();
    requestRefresh();
  }

  Future<void> addMedicine() async {
    var data = await Go().push(AddReceiveMedicinePage());
    if(data!=null){
      requestRefresh();
    }
  }

  void singleReceive(ReceiveMedicineRecordList item){
    showDialog(context: context, builder: (context){
      return Dialog(
        backgroundColor: Colors.transparent,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            SizedBox(height: 30,),
            CommonText('确认执行${item.commonName}”入柜操作吗？'),
            SizedBox(height: 30,),
            Row(
              children: [
                TextButton(onPressed: (){
                  Go().pop();
                }, child: CommonText('取消',textColor: Colors.grey),
                  style: ButtonStyle(
                      shape: WidgetStateProperty.all(StadiumBorder(side: BorderSide(color: Colors.grey)))
                  ),).setWeight(1),

                SizedBox(width: 20,),
                TextButton(onPressed: (){
                  Go().pop();
                  submitReceiveMedicine([item.id]);
                }, child: CommonText('确定',textColor: Colors.white),
                  style: ButtonStyle(
                      backgroundColor: WidgetStateProperty.all(Colors.blue),
                      shape: WidgetStateProperty.all(StadiumBorder(side: BorderSide(color: Colors.blue)))
                  ),).setWeight(1),

              ],
            ),

            SizedBox(height: 16,),
          ],
        ).addToContainer(
            padding: EdgeInsets.symmetric(horizontal: 10),
            decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: 8.borderRadius
            )
        ),
      );
    });
  }

  void batchClick(){
    if(chooseItem.isEmpty){
      showToast("请选择要入柜的药品");
      return;
    }
    var flowId = chooseItem.map((e)=>e.id).toList();
    submitReceiveMedicine(flowId);
  }

  Future<void> submitReceiveMedicine(List<dynamic> flowId) async {
    showLoadingDialog(msg: '提交中...');
    try{
      await model.receiveMedicine(flowId);
      hideDialog();
      requestRefresh();
    }catch(e){
      hideDialog();
      showToast(e.toNetError());
    }
  }
}